StepperULN2003
Loading...
Searching...
No Matches
StepperULN2003.h
Go to the documentation of this file.
1/**
2 * @file StepperULN2003.h
3 * @brief Control 28BYJ-48 stepper motor via ULN2003 driver.
4 *
5 * Supports full-step (8) and half-step (4) modes. Default is half-step.
6 * Typical full rotation: 4096 steps (adjust for gear ratio).
7 *
8 * @include StepperMotorSimple/StepperMotorSimple.ino
9 *
10 * See README for wiring and step sequence details.
11 * @author Philippe Gabriel
12 */
13
14#ifndef StepperULN2003_h
15#define StepperULN2003_h
16
17#define FULL Stepper::FULL8
18#define HALF Stepper::HALF4
19
20/**
21 * @class Stepper
22 * @brief Controls a 28BYJ-48 stepper motor via ULN2003 driver IC.
23 *
24 * This class provides an interface to control a unipolar stepper motor
25 * using the ULN2003 driver in either half-step (4) or full-step (8) mode.
26 * Step timing is managed internally based on the set speed.
27 */
28class Stepper {
29public:
30 /**
31 * @enum StepMode
32 * @brief Step resolution modes supported by the driver.
33 */
34 enum StepMode {
35 HALF4 = 4, ///< Half-step mode (4 steps per cycle)
36 FULL8 = 8 ///< Full-step mode (8 steps per cycle)
37 };
38
39 /**
40 * @brief Constructor for the Stepper class.
41 * @param steps Number of steps required for a full rotation (e.g., 4096).
42 * @param in1 Pin connected to IN1 of ULN2003.
43 * @param in2 Pin connected to IN2 of ULN2003.
44 * @param in3 Pin connected to IN3 of ULN2003.
45 * @param in4 Pin connected to IN4 of ULN2003.
46 */
47 Stepper(int steps, int in1, int in2, int in3, int in4);
48
49 /**
50 * @brief Set the motor speed in revolutions per minute (RPM).
51 * @param speed Desired speed in RPM (positive values only).
52 */
53 void setSpeed(unsigned long speed);
54
55 /**
56 * @brief Set the stepping mode (half-step or full-step).
57 * @param mode StepMode::HALF4 or StepMode::FULL8.
58 */
59 void setStepMode(StepMode mode);
60
61 /**
62 * @brief Move the motor a specified number of steps.
63 * @param steps Number of steps to move. Positive = clockwise, negative =
64 * counterclockwise.
65 */
66 void step(int steps);
67
68private:
69 /// Bit pattern for full-step sequence
70 static const unsigned int STEP_SEQUENCE[8][4];
71
72 unsigned long speed; ///< Current speed in RPM
73 unsigned long stepDelay; ///< Delay between steps in microseconds
74 unsigned int steps; ///< Total steps per revolution
75 StepMode mode; ///< Current step mode
76 unsigned int in1, in2, in3, in4; ///< Control pins
77 unsigned long lastStepTime; ///< Micros timestamp of last step
78
79 /**
80 * @brief Activate motor coils according to step index.
81 * @param stepIndex Index into the step sequence (0–7).
82 */
83 void stepMotor(int stepIndex);
84
85 /**
86 * @brief Update step delay based on current speed and step mode.
87 * @details Adjusts delay to maintain correct RPM, accounting for step mode.
88 */
89 void updateStepDelay();
90};
91
92#endif
Controls a 28BYJ-48 stepper motor via ULN2003 driver IC.
Definition StepperULN2003.h:28
void setSpeed(unsigned long speed)
Set the motor speed in revolutions per minute (RPM).
Definition StepperULN2003.cpp:42
void setStepMode(StepMode mode)
Set the stepping mode (half-step or full-step).
Definition StepperULN2003.cpp:47
StepMode
Step resolution modes supported by the driver.
Definition StepperULN2003.h:34
@ HALF4
Half-step mode (4 steps per cycle)
Definition StepperULN2003.h:35
@ FULL8
Full-step mode (8 steps per cycle)
Definition StepperULN2003.h:36
Stepper(int steps, int in1, int in2, int in3, int in4)
Constructor for the Stepper class.
Definition StepperULN2003.cpp:15
void step(int steps)
Move the motor a specified number of steps.
Definition StepperULN2003.cpp:52